import java.util.Arrays;
import java.util.Scanner;

 class NGuessNumber {
    public static int[] numbers=new int[4];
    public static int[] guesslist=new int[4];
    public static int first_num = 0;
    public static int second_num = 0;
    public static void main(String[] args) {
        while(true) {

            System.out.println("输入1开始游戏");
            System.out.println("输入0结束游戏");
            Scanner playerinput = new Scanner(System.in);

            switch (playerinput.nextInt()) {
                case 0:
                    System.out.println("退出游戏\n");
                    System.exit(0);
                    break;
                case 1:
                    int s = 0;
                    int jihui = 7;

                    System.out.println("本游戏机会只有七次");
                    generateAnswer();
                    while (true) {
                        getPlayerInput();
                        compareGuessAnswer();
                        if (isWin() == true) {
                            System.out.println("玩家获胜\n");
                            break;
                        } else {
                            s++;
                            jihui--;
                            if(s==7){
                                System.out.println("正确答案为"+Arrays.toString(numbers));
                                System.out.println("再来一局吗");
                                break;
                            }
                            System.out.println("猜错了，在来一次！(还剩"+jihui+"次)\n");

                        }
                    }
                    break;

                default:
                    System.out.println("错误的输入字符，请输入0/1\n");


            }
        }
    }

    public static int[] generateAnswer() {

        int i = 0;

        while (true) {
            int random = (int) (Math.random() * 9 + 1);
            int x = 0;
            for (; x < numbers.length; x++) {
                if (random == numbers[x]) {
                    break;
                }
            }
            if (x == numbers.length) {
                numbers[i++] = random;
            }
            if (i == 4) {
                break;
            }
        }
        return numbers;
    }

     public static int[] getPlayerInput(){

         try {
             Scanner sca = new Scanner(System.in);
             for (int i = 0; i < 4; i++) {
                 System.out.println("请输入0-9之间的第" + (i+1) + "个数字：");
                 int pl = sca.nextInt();
                 if (pl >= 10 || pl < 0) {
                     System.out.print("错误，");
                     i=i-1;
                 }else {
                     guesslist[i] = pl;
                 }
             }
         }catch (Exception e){
             System.out.println("错误");
         }

         System.out.println(Arrays.toString(guesslist)+"\n");
         return guesslist;
     }

     public static String compareGuessAnswer(){
         first_num = 0;
         second_num = 0;
         //比较玩家猜测和答案后返回提示信息。

         for (int i = 0; i < numbers.length; i++) {
             for (int l=0; l < numbers.length; l++){
                 if(i!=l){
                     if(numbers[i]==guesslist[l]){
                         second_num++;
                     }
                 }else{
                     if(numbers[i]==guesslist[l]){
                         first_num++;
                     }
                 }

             }
         }

         System.out.println(first_num+"A"+second_num+"B");
         System.out.println("A表示存在且位置正确的数字个数，B表示存在且位置不正确的数字个数！");
         System.out.println("如：1A2B,表示你猜测的四个数字有3个是对的，有一个位置对，两个位置不对。\n");

         return first_num+"A"+second_num+"B\n"+"A表示存在且位置正确的数字个数，B表示存在且位置不正确的数字个数！\n"+"如：1A2B,表示你猜测的四个数字有3个是对的，有一个位置对，两个位置不对。";
     }

     public static boolean isWin(){
         boolean flag = false;
         //根据提示信息中A的数量判断玩家是否获胜，如果获胜返回true，否则返回false。
         if(first_num==4){
             flag=true;
         }else {
             flag=false;
         }
         return flag;

     }
}

